package com.zijin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zijin.entity.Person8Turnover2;
import com.zijin.mapper.Person8Turnover2Mapper;
import com.zijin.service.Person8Turnover2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/**
 * 8人2级流水Service实现
 * */
@Service
public class Person8Turnover2ServiceImpl extends ServiceImpl<Person8Turnover2Mapper, Person8Turnover2> implements Person8Turnover2Service {

    @Autowired
    private Person8Turnover2Mapper person8Turnover2Mapper;
    /**
     * 根据交易对手卡号查流水(流水是多条用list封装)
     * */
    @Override
    public List<Person8Turnover2> findTurnoverByCounterBankNo(String counterBankNo) {
        LambdaQueryWrapper<Person8Turnover2> queryWrapper = Wrappers.<Person8Turnover2>lambdaQuery();
        queryWrapper.eq(Person8Turnover2::getCounterBankNo,counterBankNo)
                .and(i -> i.eq(Person8Turnover2::getPaymentSign,"出"))
                .orderByAsc(Person8Turnover2::getTradeTime);
        List<Person8Turnover2> person8Turnover2s = person8Turnover2Mapper.selectList(queryWrapper);
        return person8Turnover2s;
    }

    /**
     * 写一个求资金总和的方法
     * */
    @Override
    public String sumTurnoverByCounterBankNos(String counterBankNo) {
        String number = person8Turnover2Mapper.sumTurnovers(counterBankNo);
        return number;
    }

    /**
     * 写只要 交易卡号 交易户名trade_account_name 对手户名 对手卡号
     * */
    @Override
    public JSONObject findTurnoverAccountNames(String counterBankNo) {
        List<Person8Turnover2> turnoverAccountNames = person8Turnover2Mapper.findTurnoverAccountNames(counterBankNo);
        JSONObject fun = fun(turnoverAccountNames);
        return fun;
    }

    /**
     * 使用JSON工具将数据进行单独封装
     * */
    private JSONObject fun(List<Person8Turnover2> turnoverAccountNames) {
        //        使用jsonobj将数据进行单独封装
        JSONObject job = new JSONObject();
        JSONArray links = new JSONArray();
        JSONArray nodes = new JSONArray();
        HashSet<String> set = new HashSet<>();

        for (Person8Turnover2 turnoverAccountName : turnoverAccountNames) {
            JSONObject jsonObject = new JSONObject();
//            判断卡号不为空就取卡号否则就取交易账号
            if (turnoverAccountName.getTradeBankNo()!=null&&turnoverAccountName.getTradeBankNo()!=""){
//                jsonObject.put("source",turnoverAccountName.getTradeAccountName()+":"+turnoverAccountName.getTradeBankNo());
                jsonObject.put("source",turnoverAccountName.getTradeBankNo());
            }else {
                jsonObject.put("source",turnoverAccountName.getTradeAccountNo());
            }
            if (turnoverAccountName.getCounterBankNo()!=null&&turnoverAccountName.getCounterBankNo()!=""){
                jsonObject.put("target",turnoverAccountName.getCounterBankNo());
            }
            //取总金额
            jsonObject.put("value",Float.valueOf(sumTurnoverByCounterBankNos(turnoverAccountName.getCounterBankNo())));
//            给set中添加交易户名和对手户名
            set.add(turnoverAccountName.getTradeAccountName());
            set.add(turnoverAccountName.getCounterAccountName());
            links.add(jsonObject);
        }
        for (String s : set) {
            JSONObject names = new JSONObject();
            names.put("name", s);
            nodes.add(names);
        }
        job.put("links",links);
        job.put("nodes",nodes);
        return job;
    }
}
